<?php

namespace CMS\User\Models;
/**
 * Trida entity pro tabulku uzivatelskych skupin (page_group). 
 *
 * @author David Dusek
 */
use Doctrine\ORM\Mapping as ORM;

/** 
 * @ORM\Entity
 * @ORM\Table(name="user_group")
 */
class Group extends \Nette\Object {
        /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @var int 
     */
    private $id;

    /**
     * @ORM\Column(length=45)
     * @var string 
     */
    private $name;        
    
    /**
     * @ORM\OneToMany(targetEntity="\CMS\User\Models\User", mappedBy="group")
     */
    private $users;
    
    /**
     * @ORM\ManyToMany(targetEntity="Operation")
     * @ORM\JoinTable(name="user_group_operation",
     *      joinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="operation_id", referencedColumnName="id")}
     *      )
     */
    private $operations;
    
    public function __construct()
    {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();        
        $this->operations = new \Doctrine\Common\Collections\ArrayCollection();        
    }
    
    public function getId() {
        return $this->id;
    }
    
    public function setName($name) {
        $this->name = $name; 
    }

    public function getName() {
        return $this->name;
    }
    
    /* Users */
    public function addUser(User $user) {
        $this->users->add($user);
        $user->setGroup($this);
    }
    
    public function removeUser(User $user) {
        $this->users->removeElement($user);
        $user->setGroup(NULL);
    }
    
    public function getUsers() {
        return $this->users;
    }
    
    public function getUsersCount() {
        return $this->users->count();
    }
    
    /* Operations */
    public function addOperation(Operation $operation) {
        $this->operations->add($operation);
    }
    
    public function removeOperation(Operation $operation) {
        $this->operations->removeElement($operation);
    }
    
    /**
     * Operace povolene pro tuto skupinu.
     * @return \Doctrine\Common\Collections\ArrayCollection
     */
    public function getOperations() {
        return $this->operations;
    }
}
